﻿using System.Collections;
using System.Collections.Generic;
using System.Data;
using SqlCeDataAccess.Entity;

namespace SqlCeDataAccess
{
  public abstract class EntityCollectionBase<T> : IEnumerable<T>
  {
    protected List<T> EntityList { get; set; }

    public int Count
    {
      get { return EntityList.Count; }
    }

    protected EntityCollectionBase()
    {
      EntityList = new List<T>();
    }

    public void Add(T entity)
    {
      EntityList.Add(entity);
    }

    public void AddRange(IEnumerable<T> range)
    {
      EntityList.AddRange(range);
    }

    protected virtual void Load(DataTable dataTable)
    {
      if (dataTable.Rows.Count < 1)
        return;

      EntityList.Clear();
      EntityList.AddRange(dataTable.ToEntities<T>());
    }

    public IEnumerator<T> GetEnumerator()
    {
      return EntityList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
      return GetEnumerator();
    }
  }
}
